import tkinter as tk
from tkinter import messagebox, ttk
from datetime import datetime
import threading

class TimerPopup:
    def __init__(self, window):
        self.window = window
        self.popup_records = []
        self.config_file = "timer_config.txt"

        # 创建填空的小时输入框
        self.hour_label = tk.Label(window, text="小时：")
        self.hour_label.grid(row=0, column=0, padx=10, pady=5)
        self.hour_entry = ttk.Entry(window)
        self.hour_entry.grid(row=0, column=1, padx=10, pady=5)

        # 创建填空的分钟输入框
        self.minute_label = tk.Label(window, text="分钟：")
        self.minute_label.grid(row=1, column=0, padx=10, pady=5)
        self.minute_entry = ttk.Entry(window)
        self.minute_entry.grid(row=1, column=1, padx=10, pady=5)

        # 创建输入弹窗内容的文本框
        self.content_label = tk.Label(window, text="弹窗内容：")
        self.content_label.grid(row=2, column=0, padx=10, pady=5)
        self.content_entry = ttk.Entry(window)
        self.content_entry.grid(row=2, column=1, padx=10, pady=5)

        # 创建开始按钮
        self.start_button = ttk.Button(window, text="开始", command=self.start_timer)
        self.start_button.grid(row=3, column=0, columnspan=2, padx=10, pady=10)

        # 创建记录弹窗的文本框
        self.record_label = tk.Label(window, text="弹窗记录：")
        self.record_label.grid(row=4, column=0, columnspan=2, padx=10, pady=5)
        self.record_text = tk.Text(window, height=10, width=50)
        self.record_text.grid(row=5, column=0, columnspan=2, padx=10, pady=5)

        # 加载之前的定时配置
        self.load_config()

    def show_popup(self, content):
        popup_window = tk.Toplevel(self.window)
        popup_window.attributes("-topmost", True)
        popup_window.title("弹窗")
        popup_window.geometry("200x100")
        popup_label = tk.Label(popup_window, text=content)
        popup_label.pack()

    def add_record(self, time, content):
        record = f"[{time}] {content}"
        self.popup_records.append(record)
        self.record_text.insert(tk.END, record + "\n")

    def start_timer(self):
        hour = self.hour_entry.get()
        minute = self.minute_entry.get()
        content = self.content_entry.get()

        # 检查时间和内容是否有效
        if not hour or not minute:
            messagebox.showerror("错误", "请输入小时和分钟！")
            return
        if not content:
            messagebox.showerror("错误", "请输入弹窗内容！")
            return

        # 获取当前时间
        current_time = datetime.now().strftime("%H:%M")

        # 计算定时器的等待时间
        target_time = datetime.strptime(f"{hour}:{minute}", "%H:%M")
        wait_time = (target_time - datetime.strptime(current_time, "%H:%M")).total_seconds()

        # 设置定时器
        timer_thread = threading.Timer(wait_time, self.show_popup, args=(content,))
        timer_thread.start()

        # 添加记录
        self.add_record(f"{hour}:{minute}", content)

        # 保存定时配置
        self.save_config(hour, minute, content)

    def load_config(self):
        try:
            with open(self.config_file, "r") as file:
                lines = file.readlines()
                for line in lines:
                    line = line.strip()
                    if line:
                        parts = line.split(",")
                        if len(parts) == 3:
                            hour, minute, content = parts
                            self.hour_entry.insert(tk.END, hour)
                            self.minute_entry.insert(tk.END, minute)
                            self.content_entry.insert(tk.END, content)
        except FileNotFoundError:
            pass

    def save_config(self, hour, minute, content):
        with open(self.config_file, "a") as file:
            file.write(f"{hour},{minute},{content}\n")

# 创建主窗口
window = tk.Tk()

# 设置窗口标题
window.title("定时弹窗示例")

# 设置窗口大小和位置
window.geometry("400x300")
window.resizable(False, False)

# 设置窗口样式
style = ttk.Style(window)
style.configure("TLabel", font=("Arial", 12))
style.configure("TButton", font=("Arial", 12))
style.configure("TEntry", font=("Arial", 12))

# 创建定时弹窗对象
timer_popup = TimerPopup(window)

# 进入主循环
window.mainloop()